package designMode.compositePattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author LIBOFEI
 * @ClassName Menu
 * @description 菜单，包含菜单项
 * @date 2020/6/28 14:02
 * @Version 1.0
 **/
public class Menu extends MenuComponent {

    List<MenuComponent> components = new ArrayList<>();
    private String name;
    private String description;

    public Menu(String name, String description){
        this.name = name;
        this.description = description;
    }

    @Override
    public void add(MenuComponent menuComponent){
        components.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        components.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int i) {
        return components.get(i);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /** *
     * 遍历此菜单期间，如果遇到另一个菜单对象，print方法会开始另一个遍历，以此类推。
     * @author LIBOFEI
     * @date 14:10 2020/6/28
     * @return void
     * @Version  1.0
    **/
    @Override
    public void print() {
        System.out.println("name:" + getName());
        System.out.println("description:" + getDescription());
        System.out.println("----------------");
        Iterator<MenuComponent> iterator = components.iterator();
        while(iterator.hasNext()){
            MenuComponent menuComponent = iterator.next();
            menuComponent.print();
        }
    }

    @Override
    public Iterator createIterator() {
        return new CompositeIterator(components.iterator());
    }
}
